<?php
class LoginController extends Zend_Controller_Action
{
	public function preDispatch()
	{
		$this->_helper->layout->setLayout('admin_login');
	}
	
	public function loginAction()
	{
		$form = new TTF_Form_Login();
		$this->view->form = $form;
		// check for valid input
		// authenticate using adapter
		// persist user record to session
		// redirect to original request URL if present
		if ($this->getRequest()->isPost()) {
			if($form->isValid($this->getRequest()->getPost())) {
				$values = $form->getValues();
				$adapter = new TTF_Auth_Adapter_Doctrine($values['username'], $values['password']);
				$auth = Zend_Auth::getInstance();
				$result = $auth->authenticate($adapter);
				if ($result->isValid()) {
					$session = new Zend_Session_Namespace('ttf.auth');
					$session->user = $adapter->getResultArray('password');
					$session->username = $values['username'];					
					$session->logintime = date('h:i d/m/Y', time());
					if(isset($session->requestURL)) {
						$url = $session->requestURL;						
						unset($session->requestURL);
						$this->_redirect($url);
					} else {
						$this->_helper->getHelper('FlashMessenger')
										->addMessage('You were successfully logged in.');
						$this->_redirect('/admin/login/success');
					}
				} else {
					$this->view->message = 'You could not be logged in. Please try again.';
				}
			}
		}
	}
	
	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		Zend_Session::destroy();
		$this->_redirect('/admin/login');
	}
}